package com.zl.csMall.mine.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.csMall.mine.notice.mapper.NoticeMapper;
import com.zl.csMall.mine.notice.po.Notice;
import com.zl.csMall.mine.notice.service.NoticeService;
import com.zl.csMall.mine.user.po.User;
import com.zl.csMall.mine.user.service.UserService;
import com.zl.csMall.product.commodity.po.Commodity;
import com.zl.csMall.product.commodity.service.CommodityService;
import com.zl.csMall.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: NoticeServiceImpl
 * @author: ChenNing
 * @date 2025-10-24 11:10
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    public static Map<Long, User> CACHE_USER_MAP = new HashMap<>();
    public static Map<Long, Commodity> CACHE_COMMODITY_MAP = new HashMap<>();
    @Autowired
    private UserService userService;
    @Autowired
    private CommodityService commodityService;


    @Override
    public Page<Notice> getAll(Page<Notice> page) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Notice::getCreateDate);
        page = this.page(page, queryWrapper);
        List<Notice> noticeList = page.getRecords();

        int index = 0;
        for (Notice notice : noticeList){
            // 用户处理
            if (notice.getUserId() != null){
                if (CACHE_USER_MAP.get(notice.getUserId()) == null){
                    LambdaQueryWrapper<User> userQuery = new LambdaQueryWrapper<>();
                    userQuery.select(User::getId, User::getName).eq(User::getId, notice.getUserId());
                    User user = userService.getOne(userQuery);
                    notice.setUser(user);
                    CACHE_USER_MAP.put(notice.getUserId(), user);
                } else {
                    User user = CACHE_USER_MAP.get(notice.getUserId());
                    notice.setUser(user);
                }
            }
            // 商品处理
            if (notice.getCommodityId() != null){
                if (CACHE_COMMODITY_MAP.get(notice.getCommodityId()) == null){
                    LambdaQueryWrapper<Commodity> commodityQuery = new LambdaQueryWrapper<>();
                    commodityQuery.select(Commodity::getId, Commodity::getNumber, Commodity::getName).eq(Commodity::getId, notice.getCommodityId());
                    Commodity commodity = commodityService.getOne(commodityQuery);
                    notice.setCommodity(commodity);
                    CACHE_COMMODITY_MAP.put(notice.getCommodityId(), commodity);
                } else {
                    Commodity commodity = CACHE_COMMODITY_MAP.get(notice.getCommodityId());
                    notice.setCommodity(commodity);
                }
            }
            noticeList.set(index, notice);
            index++;
        }
        return page.setRecords(noticeList);
    }

    @Override
    public boolean addNotice(long userId, long commodityId, long recAddressId, long orderId) {
        Notice notice = new Notice();
        notice.setId(Util.getUuId());
        notice.setUserId(userId);
        notice.setCommodityId(commodityId);
        notice.setRecAddressId(recAddressId);
        notice.setOrderId(orderId);
        notice.setTitle("【发货通知】");
        notice.setCreateDate(new Date());
        return this.save(notice);
    }

    @Override
    public boolean deleteShipmentNotice(long orderId) {
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notice::getOrderId, orderId);
        return this.remove(queryWrapper);
    }

}
